package ambientes;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.NumberFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;

import net.java.balloontip.BalloonTip;
import net.java.balloontip.BalloonTip.AttachLocation;
import net.java.balloontip.BalloonTip.Orientation;
import net.java.balloontip.styles.BalloonTipStyle;
import net.java.balloontip.styles.RoundedBalloonStyle;
import agentes.Formula;
import agentes.FormulaDicas;
import agentes.FormulaDicasRegras;
import agentes.FormulaRegras;

public class FormulaInterface extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -646759208217349461L;

	FormulaRegras regras = new FormulaRegras();
	FormulaDicasRegras regrasDicas = new FormulaDicasRegras();
	int erros[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	
	private JFormattedTextField textField;
	private JFormattedTextField textField_1;
	private JFormattedTextField textField_2;
	private JFormattedTextField formattedTextField;
	private JFormattedTextField formattedTextField_1;
	private JFormattedTextField formattedTextField_2;
	private JFormattedTextField formattedTextField_3;
	private JFormattedTextField formattedTextField_4;
	private JFormattedTextField formattedTextField_5;
	private JFormattedTextField formattedTextField_6;
	
	JTextPane txtpnDicas;
	
	JLabel lblAjuda;

	BalloonTip balloonTip1;
	BalloonTip balloonTip2;
	BalloonTip balloonTip3;

	JButton btnValida1;
	private JButton btnDica;

	private JLabel label;

	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JPanel panel_4;
	private JPanel panel_5;

	private JLabel lbl_1;
	private JLabel lbl_2;
	private JLabel lbl_3;
	private JLabel lbl_4;
	private JLabel lbl_5;
	private JLabel lbl_6;
	private JLabel lbl_7;
	private JPanel panel_6;
	private JLabel lblPasso;
	private JLabel lblPasso_1;
	private JLabel lblPasso_2;
	private JLabel lblPasso_3;

	public FormulaInterface() {
		setAlignmentY(Component.TOP_ALIGNMENT);
		setAlignmentX(Component.LEFT_ALIGNMENT);
		setLayout(null);
		setBackground(Color.white);
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(12, 12, 956, 550);
		add(tabbedPane);

		panel_1 = new BackgroundPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel_1.setLayout(null);
		panel_1.setBounds(75, 12, 839, 182);

		JPanel panel_1_1 = new JPanel();
		panel_1_1.setBounds(55, 27, 841, 472);
		panel_1_1.setBackground(new Color(255, 255, 255, 240));
		panel_1_1.setLayout(null);

		JTextPane txtpnEnunciado = new JTextPane();
		txtpnEnunciado.setFont(new Font("Dialog", Font.PLAIN, 14));
		txtpnEnunciado.setContentType("text/html");
		txtpnEnunciado.setOpaque(false);
		txtpnEnunciado.setBounds(31, 34, 779, 390);
		txtpnEnunciado.setEditable(false);

		txtpnEnunciado
				.setText("<body style='font: 13px dialog;'>"
						+ "Pelo princípio fundamental da contagem (visto no exercício anterior) "
						+ "a quantidade de possibilidades pode ser calculada por:<br>"
						+ "<center><b>n x (n-1) x (n-2) ...</b></center> <br><br>"
						+ "Essa multiplicação de <b>n</b> ocorre <b>p</b> vezes, sendo <b>p</b> a quantidade de posições do arranjo: <br>"
						+ "<center><b>A(n,p)</b></center><br><br> "
						+ "Como fatorial <b>n! = n x (n-1) x (n-2) ... x 1</b>, podemos escrever o arranjo como: <br>"
						+ "<center><b>A(n,p) = n! / (n-p)! </b></center><br>"
						+ "<b>(n-p)!</b> é utilizado para eliminar do cálculo as possibilidades das posições que não interessam. <br><br>"
						+ "<b>Observação</b>: no caso <b>A(n,n)</b>, ou seja, <b>n</b> elementos organizados <b>n a n</b>, a fórmula ficaria: <br>"
						+ "<center><b>A(n,n) = n! / (n-n)! = n! </b></center><br>"
						+ "Esse caso de arranjo é chamado de permutação.</body>");
		panel_1_1.add(txtpnEnunciado);
		panel_1.add(panel_1_1);
		tabbedPane.add(panel_1, "Enunciado");

		panel = new BackgroundPanel();
		panel.setBounds(0, 0, 284, 420);
		panel.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel.setLayout(null);
		tabbedPane.add(panel, "Exercício");

		panel_2 = new JPanel();
		panel_2.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel_2.setLayout(null);
		panel_2.setBackground(new Color(255, 255, 255, 240));
		panel_2.setBounds(39, 12, 512, 452);
		panel.add(panel_2);

		btnValida1 = new JButton("Validar");
		btnValida1.setFont(new Font("Dialog", Font.BOLD, 14));
		btnValida1.setActionCommand("Validar");
		btnValida1.setLocation(402, 22);
		btnValida1.setSize(96, 37);

		panel_2.add(btnValida1);

		panel_6 = new JPanel();
		panel_6.setOpaque(false);
		panel_6.setBounds(43, 12, 334, 433);
		panel_2.add(panel_6);
		panel_6.setLayout(null);

		textField = new JFormattedTextField(NumberFormat.getNumberInstance());
		textField.setBounds(136, 12, 63, 43);
		panel_6.add(textField);
		textField.setDisabledTextColor(UIManager.getColor("List.background"));
		textField.setFont(new Font("Dialog", Font.PLAIN, 25));
		textField.setColumns(10);

		textField_1 = new JFormattedTextField(NumberFormat.getNumberInstance());
		textField_1.setBounds(87, 67, 63, 43);
		panel_6.add(textField_1);
		textField_1.setFont(new Font("Dialog", Font.PLAIN, 25));
		textField_1.setColumns(10);

		textField_2 = new JFormattedTextField(NumberFormat.getNumberInstance());
		textField_2.setBounds(183, 67, 63, 43);
		panel_6.add(textField_2);
		textField_2.setFont(new Font("Dialog", Font.PLAIN, 25));
		textField_2.setColumns(10);

		lbl_1 = new JLabel();
		lbl_1.setBounds(12, 18, 240, 37);
		panel_6.add(lbl_1);
		lbl_1.setFont(new Font("Dialog", Font.PLAIN, 25));
		lbl_1.setText("=                     !");

		lbl_2 = new JLabel();
		lbl_2.setBounds(38, 28, 280, 37);
		panel_6.add(lbl_2);
		lbl_2.setText("_____________________");
		lbl_2.setFont(new Font("Dialog", Font.PLAIN, 25));

		lbl_3 = new JLabel();
		lbl_3.setBounds(70, 72, 214, 37);
		panel_6.add(lbl_3);
		lbl_3.setText("(          -          )!");
		lbl_3.setFont(new Font("Dialog", Font.PLAIN, 25));

		panel_3 = new JPanel();
		panel_3.setBounds(0, 130, 334, 123);
		panel_6.add(panel_3);
		panel_3.setBackground(new Color(255, 255, 255, 0));
		panel_3.setVisible(false);
		panel_3.setBorder(null);
		panel_3.setLayout(null);

		formattedTextField = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		formattedTextField.setFont(new Font("Dialog", Font.PLAIN, 25));
		formattedTextField.setColumns(10);
		formattedTextField.setBounds(43, 25, 63, 43);
		panel_3.add(formattedTextField);

		formattedTextField_1 = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		formattedTextField_1.setFont(new Font("Dialog", Font.PLAIN, 25));
		formattedTextField_1.setColumns(10);
		formattedTextField_1.setBounds(131, 25, 63, 43);
		panel_3.add(formattedTextField_1);

		formattedTextField_2 = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		formattedTextField_2.setBounds(221, 25, 63, 43);
		panel_3.add(formattedTextField_2);
		formattedTextField_2.setFont(new Font("Dialog", Font.PLAIN, 25));
		formattedTextField_2.setColumns(10);

		formattedTextField_3 = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		formattedTextField_3.setBounds(131, 80, 63, 43);
		panel_3.add(formattedTextField_3);
		formattedTextField_3.setFont(new Font("Dialog", Font.PLAIN, 25));
		formattedTextField_3.setColumns(10);

		lbl_4 = new JLabel();
		lbl_4.setText("=           x         x         !");
		lbl_4.setFont(new Font("Dialog", Font.PLAIN, 25));
		lbl_4.setBounds(0, 31, 305, 37);
		panel_3.add(lbl_4);

		lbl_5 = new JLabel();
		lbl_5.setText("_____________________");
		lbl_5.setFont(new Font("Dialog", Font.PLAIN, 25));
		lbl_5.setBounds(26, 41, 279, 37);
		panel_3.add(lbl_5);

		lbl_6 = new JLabel();
		lbl_6.setText("!  ");
		lbl_6.setFont(new Font("Dialog", Font.PLAIN, 25));
		lbl_6.setBounds(195, 69, 33, 37);
		panel_3.add(lbl_6);
		
		lblPasso_1 = new JLabel("Passo 2");
		lblPasso_1.setBounds(0, 0, 70, 15);
		panel_3.add(lblPasso_1);

		panel_5 = new JPanel();
		panel_5.setBounds(0, 265, 334, 78);
		panel_6.add(panel_5);
		panel_5.setVisible(false);
		panel_5.setBackground(new Color(255, 255, 255, 0));
		panel_5.setLayout(null);

		formattedTextField_4 = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		formattedTextField_4.setFont(new Font("Dialog", Font.PLAIN, 25));
		formattedTextField_4.setColumns(10);
		formattedTextField_4.setBounds(43, 23, 63, 43);
		panel_5.add(formattedTextField_4);

		formattedTextField_5 = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		formattedTextField_5.setBounds(131, 23, 63, 43);
		panel_5.add(formattedTextField_5);
		formattedTextField_5.setFont(new Font("Dialog", Font.PLAIN, 25));
		formattedTextField_5.setColumns(10);

		lbl_7 = new JLabel();
		lbl_7.setText("=           x ");
		lbl_7.setFont(new Font("Dialog", Font.PLAIN, 25));
		lbl_7.setBounds(0, 29, 197, 37);
		panel_5.add(lbl_7);
		
		lblPasso_2 = new JLabel("Passo 3");
		lblPasso_2.setBounds(0, 0, 70, 15);
		panel_5.add(lblPasso_2);

		panel_4 = new JPanel();
		panel_4.setBounds(0, 362, 334, 71);
		panel_6.add(panel_4);
		panel_4.setVisible(false);
		panel_4.setBackground(new Color(255, 255, 255, 0));
		panel_4.setLayout(null);

		formattedTextField_6 = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		formattedTextField_6.setBounds(43, 23, 63, 43);
		panel_4.add(formattedTextField_6);
		formattedTextField_6.setFont(new Font("Dialog", Font.PLAIN, 25));
		formattedTextField_6.setColumns(10);

		label = new JLabel("=");
		label.setBounds(0, 28, 28, 37);
		panel_4.add(label);
		label.setFont(new Font("Dialog", Font.PLAIN, 25));
		
		lblPasso_3 = new JLabel("Passo 4");
		lblPasso_3.setBounds(0, 0, 70, 15);
		panel_4.add(lblPasso_3);
		
		lblPasso = new JLabel("Passo 1");
		lblPasso.setBounds(0, 0, 70, 15);
		panel_6.add(lblPasso);

		JPanel panel_dicas = new JPanel();
		panel_dicas.setBounds(581, 12, 328, 452);
		panel_dicas
				.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel_dicas.setBackground(new Color(255, 255, 255, 250));
		panel_dicas.setLayout(null);

		lblAjuda = new JLabel("");
		lblAjuda.setBounds(260, 12, 56, 50);
		panel_dicas.add(lblAjuda);
		lblAjuda.setIcon(new ImageIcon(FormulaInterface.class
				.getResource("/images/dica.png")));

		btnDica = new JButton("Mais Dicas!");
		btnDica.setBounds(12, 12, 114, 25);
		panel_dicas.add(btnDica);

		txtpnDicas = new JTextPane();
		txtpnDicas.setBorder(new BevelBorder(BevelBorder.LOWERED, null,
				null, null, null));
		txtpnDicas.setContentType("text/html");
		txtpnDicas.setFont(new Font("Dialog", Font.PLAIN, 14));

		txtpnDicas.setEditable(false);
		txtpnDicas
				.setText("Caso precise de ajuda, clique em \"Mais Dicas!\".");
		txtpnDicas.setBounds(22, 74, 283, 356);
		panel_dicas.add(txtpnDicas);
		panel.add(panel_dicas);

		controle();

		balloonTip1();
		balloonTip2();
		balloonTip3();

	}

	public void balloonTip1() {
		BalloonTipStyle roundedLook = new RoundedBalloonStyle(10, 10,
				Color.white, Color.LIGHT_GRAY);

		balloonTip1 = new BalloonTip(textField, new JLabel(
				"<html>Quantos bandas estão concorrendo?</html>"), roundedLook,
				Orientation.RIGHT_ABOVE, AttachLocation.ALIGNED, 30, 20, false);
		balloonTip1.setOpaque(false);
		balloonTip1.setVisible(false);
	}

	public void balloonTip2() {
		BalloonTipStyle roundedLook2 = new RoundedBalloonStyle(10, 10,
				Color.white, Color.LIGHT_GRAY);

		balloonTip2 = new BalloonTip(textField_1, new JLabel(
				"<html>Quantas bandas estão concorrendo?</html>"),
				roundedLook2, Orientation.RIGHT_BELOW, AttachLocation.ALIGNED,
				20, 20, false);
		balloonTip2.setOpaque(false);
		balloonTip2.setVisible(false);
	}

	public void balloonTip3() {
		BalloonTipStyle roundedLook2 = new RoundedBalloonStyle(10, 10,
				Color.white, Color.LIGHT_GRAY);

		balloonTip3 = new BalloonTip(textField_2, new JLabel(
				"<html>Quantas bandas serão qualificadas?</html>"),
				roundedLook2, Orientation.LEFT_BELOW, AttachLocation.ALIGNED,
				20, 20, false);
		balloonTip3.setOpaque(false);
		balloonTip3.setVisible(false);

	}

	public void balloonTipAction(BalloonTip b, boolean a) {
		b.setVisible(a);
		repaint();
	}

	public void controle() {
		btnDica.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent arg0) {
				dicas();
			}

		});

		textField.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				balloonTipAction(balloonTip1, true);
			}

			public void focusLost(FocusEvent e) {
				balloonTipAction(balloonTip1, false);
			}

		});

		textField_1.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				balloonTipAction(balloonTip2, true);
			}

			public void focusLost(FocusEvent e) {
				balloonTipAction(balloonTip2, false);
			}

		});

		textField_2.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				balloonTipAction(balloonTip3, true);
			}

			public void focusLost(FocusEvent e) {
				balloonTipAction(balloonTip3, false);
			}
		});

		btnValida1.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent arg0) {
				valida();
			}
		});
	}

	public void dicas() {
		FormulaDicas f = new FormulaDicas();

		f.setOpcao(textField, 0);
		f.setOpcao(textField_1, 1);
		f.setOpcao(textField_2, 2);
		f.setOpcao(formattedTextField, 3);
		f.setOpcao(formattedTextField_1, 4);
		f.setOpcao(formattedTextField_2, 5);
		f.setOpcao(formattedTextField_3, 6);
		f.setOpcao(formattedTextField_4, 7);
		f.setOpcao(formattedTextField_5, 8);
		f.setOpcao(formattedTextField_6, 9);
		
		f.setErros(erros);
		f.setDica(txtpnDicas);
		f.setPanel(panel_3, 0);
		f.setPanel(panel_5, 1);
		f.setPanel(panel_4, 2);
		
		regrasDicas.tell(f);
		regrasDicas.run();
		regrasDicas.retract(f);
		
		repaint();

	}

	public void valida() {
		Formula f = new Formula();

		f.setOpcao(textField, 0);
		f.setOpcao(textField_1, 1);
		f.setOpcao(textField_2, 2);
		f.setOpcao(formattedTextField, 3);
		f.setOpcao(formattedTextField_1, 4);
		f.setOpcao(formattedTextField_2, 5);
		f.setOpcao(formattedTextField_3, 6);
		f.setOpcao(formattedTextField_4, 7);
		f.setOpcao(formattedTextField_5, 8);
		f.setOpcao(formattedTextField_6, 9);

		f.setBtnValida(btnValida1);
		f.setErros(erros);
		f.setPanel(panel_3, 0);
		f.setPanel(panel_5, 1);
		f.setPanel(panel_4, 2);
		
		regras.tell(f);
		regras.run();
		regras.retract(f);
		repaint();

	}

	public int getValueResposta() {
		return Integer.parseInt(formattedTextField_6.getText());
	}
	
	public JPanel getPanelResposta() {
		return panel_4;
	}
	
}
